/**
 *
 */
package br.perfiman.faces;

import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import br.perfiman.utils.Constants;

/**
 * @author Wagner Costa
 * @since 02/01/2008
 * @version 1.0
 *
 */
public abstract class BaseBean {
	protected HttpSession session;
	protected FacesContext facesContext;
	
	public BaseBean() {
		facesContext = FacesContext.getCurrentInstance();
		session = (HttpSession) facesContext.getExternalContext().getSession(false);
	}

	public String getInternationalizationMessage(String key, String... params){
		try{
			ResourceBundle bundle = ResourceBundle.getBundle(Constants.RESOURCES_FILE_NAME, facesContext.getApplication().getDefaultLocale());
			if (params != null && params.length > 0) {
				MessageFormat mf = new MessageFormat(bundle.getString(key), facesContext.getApplication().getDefaultLocale());
				return mf.format(params, new StringBuffer(), null).toString();
			}
			return bundle.getString(key);
		} catch(MissingResourceException e){
			return "???" + key + "???";
		}
	}
	
	public void addInfoMessage(String message){
		facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, getInternationalizationMessage(message), null));
	}
	
	public void addWarnMessage(String message){
		facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, getInternationalizationMessage(message), null));
	}
	
	public void addErrorMessage(String message){
		facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, getInternationalizationMessage(message), null));
	}
	
	public void addFatalMessage(String message){
		facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, getInternationalizationMessage(message), null));
	}
}
